<template>
  <div :ref="ref" class="decoration-wrap">
    <svg :width="width" :height="height">
      <defs>
        <g :id="gId">
          <path
            v-for="(d, i) in pathD"
            :key="d"
            :stroke="pathColor[i]"
            :stroke-width="width / 2"
            fill="transparent"
            :d="d"
          />
        </g>

        <radialGradient
          :id="gradientId"
          cx="50%"
          cy="50%"
          r="50%"
        >
          <stop offset="0%" stop-color="transparent" stop-opacity="1" />
          <stop offset="100%" :stop-color="fade(colors[1] || defaultColor[1], 30)" stop-opacity="1" />
        </radialGradient>
      </defs>
      {{ circleR }}
      <circle
        v-for="r in circleR"
        :key="r"
        :r="r"
        :cx="x"
        :cy="y"
        :stroke="colors[1]"
        :stroke-width="0.5"
        fill="transparent"
      />

      <circle
        r="1"
        :cx="x"
        :cy="y"
        stroke="transparent"
        :fill="`url(#${gradientId})`"
      >
        <animate
          attributeName="r"
          :values="`1;${width / 2}`"
          :dur="`${haloDur}s`"
          repeatCount="indefinite"
        />
        <animate
          attributeName="opacity"
          values="1;0"
          :dur="`${haloDur}s`"
          repeatCount="indefinite"
        />
      </circle>

      <circle
        r="2"
        :cx="x"
        :cy="y"
        :fill="colors[1]"
      />

      <g v-if="showSplitLine">
        <polyline
          v-for="p in splitLinePoints"
          :key="p"
          :points="p"
          :stroke="colors[1]"
          :stroke-width="0.5"
          opacity="0.5"
        />
      </g>

      <path
        v-for="d in arcD"
        :key="d"
        :d="d"
        :stroke="colors[1]"
        stroke-width="2"
        fill="transparent"
      />
      <use :xlink:href="`#${gId}`">
        <animateTransform
          attributeName="transform"
          type="rotate"
          x="50"
          y="50"
          :values="`0, ${x} ${y};360, ${x} ${y}`"
          :dur="`${scanDur}s`"
          repeatCount="indefinite"
        />
      </use>
    </svg>
  </div>
</template>

<script>
import { uuid } from '../../border/util'
import { fade } from '../../border/util/fade.js'
import { getCircleRadianPoint } from '../util/util.js'
export default {
  name: 'HzxDecoration',
  props: {
    colors: {
      type: Array,
      default: () => (['#2783ce', '#2cf7fe'])
    },
    scanDur: {
      type: Number,
      default: 3
    },
    haloDur: {
      type: Number,
      default: 2
    },
    width: {
      type: Number,
      default: 150
    },
    height: {
      type: Number,
      default: 150
    }
  },
  data() {
    const id = uuid()
    return {
      ref: 'decoration-12',
      gId: `decoration-12-g-${id}`,
      gradientId: `decoration-12-gradient-${id}`,
      pathD: [],
      pathColor: [],
      circleR: [],
      splitLinePoints: [],
      arcD: [],
      segment: 10,
      sectorAngle: Math.PI / 3,
      ringNum: 3,
      ringWidth: 1,
      showSplitLine: true
    }
  },
  computed: {
    x() {
      return this.width / 2
    },
    y() {
      return this.height / 2
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.calcPathD()
      this.calcPathColor()
      this.calcCircleR()
      this.calcSplitLinePoints()
      this.calcArcD()
    },
    calcPathD() {
      const startAngle = -Math.PI / 2
      const angleGap = this.sectorAngle / this.segment
      const r = this.width / 4
      let lastEndPoints = getCircleRadianPoint(this.x, this.y, r, startAngle)
      this.pathD = new Array(this.segment)
        .fill('')
        .map((_, i) => {
          const endPoints = getCircleRadianPoint(this.x, this.y, r, startAngle - (i + 1) * angleGap).map(_ => _.toFixed(5))
          const d = `M${lastEndPoints.join(',')} A${r}, ${r} 0 0 0 ${endPoints.join(',')}`
          lastEndPoints = endPoints

          return d
        })
    },
    calcPathColor() {
      const color = this.colors[0]
      const colorGap = 100 / (this.segment - 1)
      this.pathColor = [new Array(this.segment)
        .fill('color')
        .map((_, i) => fade(color, 100 - i * colorGap))]
    },
    calcCircleR() {
      const radiusGap = (this.width / 2 - this.ringWidth / 2) / this.ringNum

      this.circleR = new Array(this.ringNum)
        .fill(0)
        .map((_, i) => radiusGap * (i + 1))
    },
    calcSplitLinePoints() {
      // const { x, y, width } = this

      const angleGap = Math.PI / 6
      const r = this.width / 2

      this.splitLinePoints = new Array(6)
        .fill('')
        .map((_, i) => {
          const startAngle = angleGap * (i + 1)
          const endAngle = startAngle + Math.PI
          const startPoint = getCircleRadianPoint(this.x, this.y, r, startAngle)
          const endPoint = getCircleRadianPoint(this.x, this.y, r, endAngle)

          return `${startPoint.join(',')} ${endPoint.join(',')}`
        })
    },
    calcArcD() {
      const angleGap = Math.PI / 6
      const r = this.width / 2 - 1
      this.arcD = new Array(4)
        .fill('')
        .map((_, i) => {
          const startAngle = angleGap * (3 * i + 1)
          const endAngle = startAngle + angleGap
          const startPoint = getCircleRadianPoint(this.x, this.y, r, startAngle)
          const endPoint = getCircleRadianPoint(this.x, this.y, r, endAngle)
          return `M${startPoint.join(',')} A${this.x}, ${this.y} 0 0 1 ${endPoint.join(',')}`
        })
    },
    fade
  }
}
</script>
<style lang="less" scoped="scoped">
.decoration-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

</style>
